package com.javaweb.dao.impl;

import com.javaweb.dao.CommentDao;
import com.javaweb.model.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class CommentDaoimpl implements CommentDao {
    @Autowired
    JdbcTemplate jdbcTemplate ;
    @Override
    public int insert(Comment newComment) {
        // 检查 article_id 是否为空
        if (newComment.getArticleId() == null) {
            throw new IllegalArgumentException("ArticleId cannot be null");
        }

        // SQL 插入语句
        String sql = "INSERT INTO tb_comment (article_id, user_id, content,Oppose,Support, create_time,update_time) " +
                "VALUES (?, ?, ?, ?,?, ?, ?)";

        // 插入参数
        Object[] args = new Object[] {
                newComment.getArticleId(),
                newComment.getUserId(),
                newComment.getContent(),
                newComment.getOppose(),
                newComment.getSupport(),
                new java.sql.Timestamp(System.currentTimeMillis()),  // 设置当前时间作为创建时间
                new java.sql.Timestamp(System.currentTimeMillis())
    };

        // 执行插入操作
        int rowsAffected = jdbcTemplate.update(sql, args);

        // 如果插入成功，返回1；否则返回0
        return rowsAffected > 0 ? 1 : 0;
    }
@Override
public int insertSupport(Integer commentId) {
    // 检查 commentId 是否为空
    if (commentId == null) {
        throw new IllegalArgumentException("CommentId cannot be null");
    }

    // SQL 更新语句，增加点赞数
    String sql = "UPDATE tb_comment SET support_count = support_count + 1 WHERE article_id = commentId";

    // 执行更新操作
    int rowsAffected = jdbcTemplate.update(sql, commentId);

    // 返回值：如果更新成功，返回1；否则返回0
    return rowsAffected > 0 ? 1 : 0;
}
@Override
public int insertOppose(Integer commentId) {
    // 检查 commentId 是否为空
    if (commentId == null) {
        throw new IllegalArgumentException("CommentId cannot be null");
    }

    // SQL 更新语句，增加点差数
    String sql = "UPDATE tb_comment SET oppose_count = oppose_count + 1 WHERE id = ?";

    // 执行更新操作
    int rowsAffected = jdbcTemplate.update(sql, commentId);

    // 返回值：如果更新成功，返回1；否则返回0
    return rowsAffected > 0 ? 1 : 0;
}
@Override
public List<Comment> selectAllCommentsById(Integer articleId) {
    // 检查 articleId 是否为空
    if (articleId == null) {
        throw new IllegalArgumentException("ArticleId cannot be null");
    }

    // SQL 查询语句
    String sql = "SELECT * FROM tb_comment WHERE article_id = ? ORDER BY create_time DESC";

    // 执行查询并映射结果
    List<Comment> comments = jdbcTemplate.query(sql, new Object[]{articleId}, new RowMapper<Comment>() {
        @Override
        public Comment mapRow(ResultSet rs, int rowNum) throws SQLException {
            Comment comment = new Comment();
            comment.setId(rs.getInt("id"));
            comment.setArticleId(rs.getInt("article_id"));
            comment.setUserId(rs.getInt("user_id"));
            comment.setContent(rs.getString("content"));

            comment.setCreateTime(rs.getTimestamp("create_time"));
            return comment;
        }
    });

    // 返回结果：如果查询为空，则返回 null，否则返回评论列表
    return comments.isEmpty() ? null : comments;
}


}
